﻿using Aliyun.OSS;
using Microsoft.AspNetCore.Mvc;
using store.cyt369.com.models;
using store.cyt369.com.models.Base;
using store.cyt369.com.MODELS;
using System.Buffers.Text;

namespace store.cyt369.com.Controllers
{
    public class SearchController : Controller
    {
        public IActionResult Index(string id)
        {
            string base64str = id;
            int total = 0;

            //行数，页数，当前页面的父类ID
            int page = 1, rows, parentCategoryID = 0;
            rows = store.cyt369.com.utility.SystemSettingService.Instance.PageSize;
            //当前选择的行业ID，选择的区域，选择的面积
            int StoreCategorysID = 0, RegionID = 0, Area = 0;
            string searchStr = null;
            if(!string.IsNullOrEmpty(base64str))
            {
                var argsJson=MODELS.Base64.Base64ToJSON(base64str);
                page=argsJson.GetInteger("page",1);
                StoreCategorysID = argsJson.GetInteger("StoreCategorysID", 0);
                parentCategoryID = argsJson.GetInteger("parentCategoryID", 0);
                RegionID = argsJson.GetInteger("RegionID", 0);
                Area = argsJson.GetInteger("Area", 0);
                searchStr = argsJson.GetString("searchStr");
            }

            return SearchView("Index",page,rows,parentCategoryID,StoreCategorysID,RegionID,Area,searchStr);
        }

        /// <summary>
        /// JS调用入口
        /// </summary>
        /// <returns></returns>
        public IActionResult Index2()
        {
            int page = Request.Query.GetInteger("page", 1);
            int rows = store.cyt369.com.utility.SystemSettingService.Instance.PageSize;
            int? StoreCategorysID = Request.Query.GetInteger("StoreCategorysID", 0);
            int parentCategoryID = Request.Query.GetInteger("parentCategoryID", 0);
            int? Area = Request.Query.GetInteger("Area", 0);
            int? RegionID = Request.Query.GetInteger("RegionID", 0);
            string searchStr = Request.Query.GetString("searchStr");
            return SearchView("Index", page, rows, parentCategoryID, StoreCategorysID, Area, RegionID, searchStr);
        }

        public IActionResult SearchView(string viewName, int page, int row, int parentCategoryID, int? StoreCategorysID, int? RegionID, int? Area, string searchStr)
        {
            int total = 0;
            IQueryable<Store> stores;
            IQueryable<Store> stores2;
            //初始化行业
            if (StoreCategorysID.HasValue && StoreCategorysID != 0)//如果选择行业存在
            {
                StoreCategory storeCategory = store.cyt369.com.dal.StoreCategoryService.Instance.StoreCategorysGetByID(StoreCategorysID);
                var parentCategory = dal.StoreCategoryService.Instance.StoreCategorysGetByParentID_InclueChilds(storeCategory.ParentID);
                ViewBag.StoreCategory = parentCategory;
                //获取查询结果
                stores = dal.StoreService.Instance.GetStore(page: page, rows: row, ref total, searchStr: searchStr, categoryID: StoreCategorysID, area: Area, cityID: RegionID);
                stores2=dal.StoreService.Instance.GetStores_NoIncludeImg(searchStr:searchStr,categoryID:StoreCategorysID,area:Area,cityID:RegionID);
            }
            else if (parentCategoryID!=0)//如果选择的‘不限’
            {
                var parentCategory = dal.StoreCategoryService.Instance.StoreCategorysGetByParentID_InclueChilds(parentCategoryID);
                ViewBag.StoreCategory = parentCategory;
                //获取查询结果
                stores = store.cyt369.com.dal.StoreService.Instance.GetStore(page: page, rows: row, ref total, searchStr: searchStr, categoryID: StoreCategorysID, area: Area, cityID: RegionID);
                stores2 = store.cyt369.com.dal.StoreService.Instance.GetStores_NoIncludeImg(searchStr: searchStr, categoryID: parentCategoryID, area: Area, cityID: RegionID);
            }
            else
            {
                var allParentCategorys = store.cyt369.com.dal.StoreCategoryService.Instance.StoreCategorysGetByParentID(0);//轮播左侧的类别
                ViewBag.allParentCategorys = allParentCategorys;
                //获取查询结果
                stores = store.cyt369.com.dal.StoreService.Instance.GetStore(page: page, rows: row, ref total, searchStr: searchStr, categoryID: StoreCategorysID, area: Area, cityID: RegionID);
                stores2 = store.cyt369.com.dal.StoreService.Instance.GetStores_NoIncludeImg(searchStr: searchStr, categoryID: StoreCategorysID, area: Area, cityID: RegionID);
            }
            var citys = new List<store.cyt369.com.models.Base.City>();
            //初始化城市
            if (stores2 != null)
            {
                var dict = new Dictionary<int?, int>();
                foreach (var b in stores2)
                {
                    //if(b.FranchiseRegionID!=null)
                    if (!dict.ContainsKey(b.CityID))
                    {
                        dict[b.CityID] = 0;
                        var city = store.cyt369.com.dal.CityService.Instance.GetCityByID(b.CityID);
                        if (city != null) { citys.Add(city); }
                    }
                }
            }

            ViewBag.citys = citys;
            ViewBag.searchStr = searchStr;
            ViewBag.storescategoryID = StoreCategorysID;
            ViewBag.area = Area;
            ViewBag.stores = stores;
            ViewBag.total = total;
            ViewBag.page = page;
            return View(viewName);

        }
    }
}
